package com.xsq.newmall01.ServiceImpl.MerchantUserServiceImpl;

import com.xsq.newmall01.Dao.MerchantUser;
import com.xsq.newmall01.Exception.ServiceException;
import com.xsq.newmall01.Mapper.MerchantMapper;
import com.xsq.newmall01.Service.MerchantUserService.MRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("merchantregisterservice")
public class MRegisterServiceImpl implements MRegisterService {
    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public void registerMerchantUser(MerchantUser merchantUser) {
        // 1. 校验商家用户名重复
        if (merchantMapper.checkMerchantUsernameExists(merchantUser.getUsername())) {
            throw new ServiceException("商家用户名已存在");
        }
        // 2. 校验手机号格式
        if (merchantUser.getPhone() != 0 && !isValidPhone(String.valueOf(merchantUser.getPhone()))) {
            throw new ServiceException("手机号格式错误（需11位数字）");
        }
        merchantUser.setStatus(1);
        // 3. 执行注册
        merchantMapper.insertmerchantuser(merchantUser);
    }

    private boolean isValidPhone(String phone) {
        return phone.matches("^1[3-9]\\d{9}$");
    }
}
